package jmine.tec.utils.io.http.impl;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.Map;

import jmine.tec.utils.Tuple;
import jmine.tec.utils.collection.MultiMap;
import jmine.tec.utils.io.http.Connector;
import jmine.tec.utils.io.http.HttpMethod;
import jmine.tec.utils.io.http.exception.HttpException;

/**
 * Connector que delega todas as suas chamadas.
 * 
 * @author lundberg
 */
public abstract class AbstractDelegatingConnector implements Connector {

    /**
     * {@inheritDoc}
     */
    public void connect() throws IOException, HttpException {
        this.getDelegate().connect();
    }

    /**
     * {@inheritDoc}
     */
    public boolean isConnected() {
        return this.getDelegate().isConnected();
    }

    /**
     * {@inheritDoc}
     */
    public InputStream makeGet(String path, Map<String, String> params) throws IOException, HttpException {
        return this.getDelegate().makeGet(path, params);
    }

    /**
     * {@inheritDoc}
     */
    public InputStream makePost(String path, Map<String, String> params) throws IOException, HttpException {
        return this.getDelegate().makePost(path, params);
    }

    /**
     * {@inheritDoc}
     */
    public InputStream makePut(String path, Map<String, String> params) throws IOException, HttpException {
        return this.getDelegate().makePut(path, params);
    }

    /**
     * {@inheritDoc}
     */
    public InputStream makeDelete(String path, Map<String, String> params) throws IOException, HttpException {
        return this.getDelegate().makeDelete(path, params);
    }

    /**
     * {@inheritDoc}
     */
    public Reader makeGetStringResponse(String path, Map<String, String> params) throws IOException, HttpException {
        return this.getDelegate().makeGetStringResponse(path, params);
    }

    /**
     * {@inheritDoc}
     */
    public Reader makePostStringResponse(String path, Map<String, String> params) throws IOException, HttpException {
        return this.getDelegate().makePostStringResponse(path, params);
    }

    /**
     * {@inheritDoc}
     */
    public Reader makePutStringResponse(String path, Map<String, String> params) throws IOException, HttpException {
        return this.getDelegate().makePutStringResponse(path, params);
    }

    /**
     * {@inheritDoc}
     */
    public Reader makeDeleteStringResponse(String path, Map<String, String> params) throws IOException, HttpException {
        return this.getDelegate().makeDeleteStringResponse(path, params);
    }

    /**
     * {@inheritDoc}
     */
    public void close() {
        this.getDelegate().close();
    }

    /**
     * {@inheritDoc}
     */
    public InputStream makeMultipartPost(String path, MultiMap<String, File> files, MultiMap<String, String> params) throws IOException,
            HttpException {
        return this.getDelegate().makeMultipartPost(path, files, params);
    }

    /**
     * {@inheritDoc}
     */
    public Reader makeMultipartPostStringResponse(String path, MultiMap<String, File> files, MultiMap<String, String> params)
            throws IOException, HttpException {
        return this.getDelegate().makeMultipartPostStringResponse(path, files, params);
    }

    /**
     * {@inheritDoc}
     */
    public Tuple<InputStream, MultiMap<String, String>> makeRequestWithBody(String path, HttpMethod method,
            MultiMap<String, String> headerParameters, MultiMap<String, String> queryParameters, InputStream requestBody)
            throws HttpException, IOException {
        return this.getDelegate().makeRequestWithBody(path, method, headerParameters, queryParameters, requestBody);
    }

    /**
     * {@inheritDoc}
     */
    public Tuple<InputStream, MultiMap<String, String>> makeRequest(String path, HttpMethod method,
            MultiMap<String, String> headerParameters, MultiMap<String, String> queryParameters) throws HttpException, IOException {
        return this.getDelegate().makeRequest(path, method, headerParameters, queryParameters);
    }

    /**
     * {@inheritDoc}
     */
    public Reader makeRequestStringResponse(String path, HttpMethod method, MultiMap<String, String> headerParameters,
            MultiMap<String, String> queryParameters, InputStream requestBody) throws HttpException, IOException {
        return this.getDelegate().makeRequestStringResponse(path, method, headerParameters, queryParameters, requestBody);
    }

    /**
     * {@inheritDoc}
     */
    public Reader makeRequestStringResponse(String path, HttpMethod method, MultiMap<String, String> headerParameters,
            MultiMap<String, String> queryParameters) throws HttpException, IOException {
        return this.getDelegate().makeRequestStringResponse(path, method, headerParameters, queryParameters);
    }

    /**
     * {@inheritDoc}
     */
    public String encodeParameters(MultiMap<String, String> queryParameters) throws IOException {
        return this.getDelegate().encodeParameters(queryParameters);
    }

    /**
     * Obtém uma referência ao conector que deve tratar as requisições.
     * 
     * @return delegate
     */
    protected abstract Connector getDelegate();
}
